import Vue from 'vue'
import Vuex from "vuex"
import {ADD,SUB} from "./mutations-type"

// 1.引入vuex
Vue.use(Vuex);
//2.创建仓库
let store = new Vuex.Store({
  state: {
    counter: 777
  },
  mutations: {
    [ADD]: function (state,step) {
      state.counter +=step;
    },
    [SUB]: function (state,payload) {
      console.log(payload);
      state.counter+=payload.payload.step;
    }
  },
  actions: {
    asyncAdd: function (store,step) {
      setTimeout(function () {
        store.commit(ADD,step);
      }, 3000)
    }

  },
  //vuex 中的计算属性
  getters:{
    isOddOrEven: function(state){
      console.log(state);
      return state.counter %2===0 ? "偶数" : "奇数"
    }      
  }
});

// 3.导出仓库
export default store;